package com.gulimall.product.app;

import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.gulimall.product.service.impl.SkuInfoServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.gulimall.product.entity.SkuInfoEntity;
import com.gulimall.product.service.SkuInfoService;
import com.common.utils.PageUtils;
import com.common.utils.R;

import javax.annotation.Resource;


/**
 * sku信息
 *
 * @author Rookie-886
 * @email ${email}
 * @date 2021-03-14 16:30:53
 */
@RestController
@RequestMapping("product/skuinfo")
public class SkuInfoController {
    @Resource
    private SkuInfoServiceImpl skuInfoServiceImpl;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("product:skuinfo:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = skuInfoServiceImpl.queryPage(params);

        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{skuId}")
//    @RequiresPermissions("product:skuinfo:info")
    public R info(@PathVariable("skuId") Long skuId){
		SkuInfoEntity skuInfo = skuInfoServiceImpl.getById(skuId);

        return R.ok().put("skuInfo", skuInfo);
    }

    /**
     * 获取批量信息
     */
    @RequestMapping("/infoList")
//    @RequiresPermissions("product:skuinfo:info")
    public R infoList(@RequestBody List<Long> skuIdList){
        Map<Long,SkuInfoEntity> map = new HashMap<>();
        List<SkuInfoEntity> skuInfoList = skuInfoServiceImpl.listByIds(skuIdList);
        for (SkuInfoEntity skuInfoEntity : skuInfoList) {
            map.put(skuInfoEntity.getSkuId(), skuInfoEntity);
        }
        return R.ok().put("data", map);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
//    @RequiresPermissions("product:skuinfo:save")
    public R save(@RequestBody SkuInfoEntity skuInfo){
        skuInfoServiceImpl.save(skuInfo);

        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
//    @RequiresPermissions("product:skuinfo:update")
    public R update(@RequestBody SkuInfoEntity skuInfo){
        skuInfoServiceImpl.updateById(skuInfo);

        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
//    @RequiresPermissions("product:skuinfo:delete")
    public R delete(@RequestBody Long[] skuIds){
        skuInfoServiceImpl.removeByIds(Arrays.asList(skuIds));

        return R.ok();
    }

}
